import requests
from lxml import etree

url = 'http://angelimg.spbeen.com'

headers = {
    "Referer": 'http://angelimg.spbeen.com'
}

response = requests.get(url)
html = etree.HTML(response.text)

image_links = html.xpath('.//img/@src')

# 循环获取所有的链接地址
# for link in image_links:
#     with open('images.txt', mode='w', encoding='utf8') as file:
#         file.write(link)

# with open('images3.txt', mode='w', encoding='utf8') as file:
#     for link in image_links:
#         file.write(link)
#         file.write('\n')


for index, link in enumerate(image_links, start=1):
    imagename = link.split('/')[-1]

    filename = "images/{}_{}".format(index, imagename)
    print(filename, link)
    try:
        image_response = requests.get(link, headers=headers)
        with open(filename, mode='wb') as image:
            image.write(image_response.content)
        print(index, 'success', link)
    except:
        print(index, 'error', link)
